package com.leopardcat.firewall;

import com.android.internal.telephony.ITelephony;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class FirewallService extends Service {

	private IncomingCallReceiver mIncomingCallReceiver;
	//private ITelephony			 mITelephony;
	private String[]			 mSMSBlackList;
	
	public static boolean				 mIsRunning = false;				
	
	
	@Override
	public void onCreate() {
		// register incoming call receiver
		super.onCreate();
		
        mIncomingCallReceiver = new IncomingCallReceiver();
        
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction("android.intent.action.PHONE_STATE");
        mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
        mIntentFilter.addAction("android.provider.Telephony.WAP_PUSH_RECEIVED");
        
        registerReceiver(mIncomingCallReceiver, mIntentFilter);
        //mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        
        reloadSMSBlackList();
        
        mIsRunning = true;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(mIncomingCallReceiver); //disconnect listener
		
		mIsRunning = false;
	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	
	public void reloadSMSBlackList() {
		SMSBlackListDB	db = new SMSBlackListDB(this);
		Cursor cursor = db.select();
		mSMSBlackList = new String[cursor.getCount()];
		for( int i = 0; i < cursor.getCount(); i++ ) {
			cursor.moveToPosition(i);
			mSMSBlackList[i] = cursor.getString(1);
		}
		
		db.close();
	}
	
	public boolean isBlackNumber(String number) {
		for( int i = 0; i < mSMSBlackList.length; i++ ) {
			if( number.equals(mSMSBlackList[i]) ) {
				return true;
			}
		}
		return false;
	}
	
	   /** incoming call receiver */
    private class IncomingCallReceiver extends BroadcastReceiver {
    	/** */
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		String action = intent.getAction();
    		Log.d("leopard", "Action:" + action);
    		
//    		if( action.equals("android.intent.action.PHONE_STATE") ) {
//    			Log.d("leopard", "incoming call...");
//    			String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
//    			
//    			String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
//    			
//    			if( state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING) ) {
//    				if( number.equals("111") ) {
//    					
//    					// hangout incoming
//    					try {
//							mITelephony.endCall();
//						} catch (RemoteException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//    					
//    				}
//    			}
//    			
//    		} else
    		if ( action.equals("android.provider.Telephony.SMS_RECEIVED") ) {
				SmsMessage sms = getMessagesFromIntent(intent)[0];
				String number = sms.getOriginatingAddress();
				String content = sms.getDisplayMessageBody();
				Log.d("leopard", "incoming number: " + number);
				if( isBlackNumber(number) ) {
					Toast.makeText(context, number+content, Toast.LENGTH_LONG).show();
					abortBroadcast();
				}
			}
    		else if ( action.equals("android.provider.Telephony.WAP_PUSH_RECEIVED") ) {
    			Toast.makeText(context, "mms recieved", Toast.LENGTH_LONG).show();
    		}
			
    	}
    }
    
    public final static SmsMessage[] getMessagesFromIntent(Intent intent) {
    	Object[] messages = (Object[]) intent.getSerializableExtra("pdus");
    	byte [][] pduObjs = new byte [messages.length][];
    	for( int i = 0; i < messages.length; i++ ) {
    		pduObjs[i] = (byte[]) messages[i];
    	}
    	
    	byte [][] pdus = new byte[pduObjs.length][];
    	int pduCount = pdus.length;
    	SmsMessage[] msgs = new SmsMessage[pduCount];
    	
    	for( int i = 0; i < pduCount; i++ ) {
    		pdus[i] = pduObjs[i];
    		msgs[i] = SmsMessage.createFromPdu(pdus[i]);
    	}
    	
    	return msgs;
    }
   	

}
